package com.qf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feign.HotalFeign;
import com.qf.aop.UserUtil;
import com.qf.dao.OrdersDao;
import com.qf.entity.OrderDetailResult;
import com.qf.entity.OrderParams;
import com.qf.entity.Orders;
import com.qf.entity.User;
import com.qf.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * (Orders)表服务实现类
 *
 * @author makejava
 * @since 2020-11-30 14:14:36
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements OrdersService {

    @Autowired
    private HotalFeign hotalFeign;

    /**
     * 进行下单
     * @param orders
     * @param orderParams
     * @return
     */
    @Override
    public String insertOrder(Orders orders, OrderParams orderParams) {

        //设置订单号
        //yyMMdd + 用户id后4位 + redis - id自增序列
        orders.setOid(UUID.randomUUID().toString().replace("-", ""));

        //设置用户id
        User user = UserUtil.getUser();
        orders.setUid(user.getId());

        //设置总价格
        OrderDetailResult result = hotalFeign.getRoomPriceFeign(orderParams).getData();
        double allPrice = result.getAllPrice();
        orders.setAllPrice(BigDecimal.valueOf(allPrice));

        //保存订单
        super.save(orders);
        return orders.getOid();
    }
}